<?php

namespace app\user\controller;

class mydata extends base
{

    public function _initialize()
    {
        $this->model = model('user');
    }

    public function index()
    {
        $data = $this->model->where('id', $this->getLoginUser()->id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * @return mixed|\think\response\Json
     * 修改用户密码
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $result = $this->validate($data, 'user.edit');
            if (true !== $result) {
                $this->success($result);
                return ajax_info($result);
            }

            if ($this->getLoginUser()->password !== md5($data['old_password'] . $this->getLoginUser()->code) || empty($data['password'])) {
                $this->success('密码错误');
                return ajax_info('密码错误');
            }
            $data['password'] = md5($data['password'] . $this->getLoginUser()->code);
            $this->getLoginUser()->password = $data['password'];

            $user = $this->model->allowField(true)->save($data, ['id' => $this->getLoginUser()->id]);
            if ($user) {
                $this->success(lang('Successfully modified'));
                return ajax_info(lang('Successfully modified'), 'y');
            }
            $this->error(lang('fail to edit'));
            return ajax_info(lang('fail to edit'));
        }
        return $this->fetch();

    }

    /**
     * @return \think\response\Json
     * 修改用户信息
     */
    public function save()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();

//            ($this->getLoginUser()->ismail=='1')?'':$data['status'] = 0;

            $result = $this->validate($data, 'user.edit');
            if (true !== $result) {
              //  $this->success($result);
                return ajax_info($result);
            }

            $data['lang']=cookie('think_var');
            $user = $this->model->allowField(true)->save($data, ['id' => $this->getLoginUser()->id]);
            if ($user) {
               // $this->success(lang('Successfully modified'));
                return ajax_info(lang('Successfully modified'), 'index');

            }
            $this->error(lang('fail to edit'));
            return ajax_info(lang('fail to edit'));
        }

        return ajax_info(lang('Connection does not exist'));

    }
}
